/*******************************************************************************
 * Copyright 2012 Fatminds, Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.fatminds.vaadin_cmis_integration.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.fatminds.cmis.form.CmisFormTemplate;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;

public class CMS_DataLists extends CustomComponent implements CmisFormTemplate{

	@AutoGenerated
	private AbsoluteLayout mainLayout;
	@AutoGenerated
	private Table tblDataItems;
	@AutoGenerated
	private Button btnDeleteDataItem;
	@AutoGenerated
	private Button btnAddDataItem;
	@AutoGenerated
	private TextField txtDataItemToAddEdit;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public CMS_DataLists() {
		buildMainLayout();
		setCompositionRoot(mainLayout);

		// TODO add user code here
		
		
		tblDataItems.addContainerProperty("Select Data Item", String.class,  null);
		
		// Allow selecting items from the table.
		tblDataItems.setSelectable(true);

		// Send changes in selection immediately to server.
		tblDataItems.setImmediate(true);
		
		
		// Handle selection change.
		tblDataItems.addListener(new Property.ValueChangeListener() {
		    public void valueChange(ValueChangeEvent event) {
		    	txtDataItemToAddEdit.setValue(tblDataItems.getValue());
		        System.out.println("Selected: " + tblDataItems.getValue());
		    }
		});
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		//
		// End of functionality testing code
		//
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	
}

	public HashMap<String, Component> getCmisFieldComponents() {
		HashMap<String, Component> componentMap = new HashMap<String, Component>();
		componentMap.put("cmis:name", txtDataItemToAddEdit);
		return componentMap;
	}

	public List<ComponentContainer> getComponentContainers() {
		List<ComponentContainer> layouts = new ArrayList<ComponentContainer>();
		layouts.add(mainLayout);
		return layouts;
	}

	@AutoGenerated
	private AbsoluteLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("400px");
		mainLayout.setHeight("250px");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("400px");
		setHeight("250px");
		
		// txtDataItemToAddEdit
		txtDataItemToAddEdit = new TextField();
		txtDataItemToAddEdit.setImmediate(false);
		txtDataItemToAddEdit.setWidth("324px");
		txtDataItemToAddEdit.setHeight("24px");
		txtDataItemToAddEdit.setInputPrompt("Enter New Data Item");
		txtDataItemToAddEdit.setSecret(false);
		mainLayout.addComponent(txtDataItemToAddEdit,
				"top:170.0px;left:40.0px;");
		
		// btnAddDataItem
		btnAddDataItem = new Button();
		btnAddDataItem.setCaption("Add/Update Data Item");
		btnAddDataItem.setImmediate(true);
		btnAddDataItem.setWidth("-1px");
		btnAddDataItem.setHeight("-1px");
		mainLayout.addComponent(btnAddDataItem, "top:200.0px;left:40.0px;");
		
		// btnDeleteDataItem
		btnDeleteDataItem = new Button();
		btnDeleteDataItem.setCaption("Delete Selected Data Item");
		btnDeleteDataItem.setImmediate(true);
		btnDeleteDataItem.setWidth("-1px");
		btnDeleteDataItem.setHeight("-1px");
		mainLayout.addComponent(btnDeleteDataItem, "top:200.0px;left:200.0px;");
		
		// tblDataItems
		tblDataItems = new Table();
		tblDataItems.setImmediate(false);
		tblDataItems.setWidth("324px");
		tblDataItems.setHeight("163px");
		mainLayout.addComponent(tblDataItems, "left:40.0px;");
		
		return mainLayout;
	}

}
